package com.lv.phuotbacnam.bl.queries;

import java.util.List;

import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class NhanXetQueries {

	/**
	 * To create new nhan xet
	 * 
	 * @param nhan_xet
	 * @param baidang
	 * @param callBack
	 */
	public static void createNhanXet(String nhan_xet, ParseObject baidang,
			final CallBackFromParse<Object, Object> callBack) {
		ParseObject nhanxet = new ParseObject(
				QueriesConstant.TableNhanXet.Table_Name);
		nhanxet.put(QueriesConstant.TableNhanXet.COLUMN_noi_dung, nhan_xet);
		nhanxet.put(QueriesConstant.TableNhanXet.COLUMN_nguoi_nhan_xet,
				ParseUser.getCurrentUser());
		nhanxet.put(QueriesConstant.TableNhanXet.COLUMN_bai_dang, baidang);
		nhanxet.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				if (e == null)
					callBack.done("", "");
			}
		});

	}

	/**
	 * To get all nhan xet in baidang
	 * 
	 * @param baidang
	 * @param callBack
	 */
	public static void getListNhanXet(ParseObject baidang,
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableNhanXet.Table_Name);
		query.whereEqualTo(QueriesConstant.TableNhanXet.COLUMN_bai_dang,
				baidang);
		query.addDescendingOrder(QueriesConstant.TableNhanXet.COLUMN_createdAt);
		query.include(QueriesConstant.TableNhanXet.COLUMN_nguoi_nhan_xet);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> nhanxets, ParseException e) {
				if (e == null && nhanxets.size() > 0)
					callBack.done("", nhanxets);
				else
					callBack.done("", null);
			}
		});

	}
}
